﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace ProjectNocturnalFool
{
    class PartyPortraitControl : ButtonControl
    {
        #region Fields
        protected Texture2D portraitTexture;

        public int characterSlot = Constants.INDEX_INVALID;
        #endregion

        #region Overrides

        /// <summary>
        /// Loads Content of button
        /// </summary>
        public override void LoadContent()
        {
            base.LoadContent();

            _bgTexture = TacticsGame.Instance.Content.Load<Texture2D>(Constants.TextureWhite);
            //HACK
            portraitTexture = TacticsGame.Instance.Content.Load<Texture2D>(Constants.TexturePortraitDust);
            font = TacticsGame.Instance.gameFont;
            message = "";

            width = Constants.PartyMemberWidth;
            height = Constants.PartyMemberHeight;
            depth = ControlDepth.NonDrag;
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            portraitTexture = IconLoader.loadRaceIcon(GameLogic.Instance.gameCharacterSession.charList[characterSlot].charRace);
        }

        public override void clickHandle()
        {
            if (InputMgr.Instance.isMouseClickUp(Click.Left) && Hover)
            {
                GameLogic.Instance.gameStateLogic.state =  GameStateMessage.EditCharacter;
                GameLogic.Instance.gameStateLogic.selectedChar = characterSlot;
            }
        }

        /// <summary>
        /// Draws button based on location
        /// </summary>
        /// <param name="gameTime">gameTime since last draw</param>
        public override void Draw(GameTime gameTime)
        {
            int centrePortrait = Util.getCentreDrawPointX(x, width, Constants.PortraitWidth);

            Rectangle targetPortraitRect = new Rectangle(centrePortrait, y + Constants.ControlSmallOffsets, Constants.PortraitWidth, Constants.PortraitHeight);
            Rectangle targetBgRect = new Rectangle(x, y, width, height);

            TacticsGame.Instance.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullNone);
            
            TacticsGame.Instance.spriteBatch.Draw(_bgTexture,
                                                 targetBgRect, null,
                                                 Color.Gray * 0.3f,
                                                 0,
                                                 Vector2.Zero,
                                                 SpriteEffects.None,
                                                 0);


            TacticsGame.Instance.spriteBatch.Draw(portraitTexture,
                                                  targetPortraitRect, null,
                                                  Color.White,
                                                  0,
                                                  Vector2.Zero,
                                                  SpriteEffects.None,
                                                  0);

            TacticsGame.Instance.spriteBatch.End();

            //Hover Logic
            TacticsGame.Instance.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Additive, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullNone);

            if (Hover)
            {
                TacticsGame.Instance.spriteBatch.Draw(_bgTexture,
                                                       targetBgRect,
                                                       null,
                                                       Color.Red,
                                                       0,
                                                       Vector2.Zero,
                                                       SpriteEffects.None,
                                                       0);
            }

            TacticsGame.Instance.spriteBatch.End();
        }

        #endregion

    }
}
